package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.event.SubmitBankCardEvent;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.ui.dialog.SendVerificationCodeDialog;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.TimeCount;
import com.chain.BrotherExchange.viewmodel.PaymentViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

/**
 * 添加银行卡
 */
public class AddBankCardActivity extends BaseActivity {

    @Bind(R.id.name_edit) EditText nameEdit;
    @Bind(R.id.deposit_bank_edit) EditText depositBankEdit;
    @Bind(R.id.open_account_address_edit) EditText openAccountAddressEdit;
    @Bind(R.id.bank_card_number_edit) EditText bankCardNumberEdit;
    @Bind(R.id.bank_card_number_again_edit) EditText bankCardNumberAgainEdit;
    @Bind(R.id.apply_button) Button applyButton;

    private PaymentViewModel mPaymentViewModel;

    public static void launch(Context context, int type) {
        context.startActivity(new Intent(context, AddBankCardActivity.class)
                .putExtra(C.EXTRA_PAGE_TYPE, type));
    }

    @Override
    protected int getLayoutID() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return R.layout.activity_add_bank_card;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_add_bank_card);

        mPaymentViewModel = ViewModelProviders.of(this).get(PaymentViewModel.class);

        // 银行卡信息
        loadBankCardInfo();

        applyButton.setOnClickListener(v -> startSubmitBankCard());
    }

    private void startSubmitBankCard() {
        String name = nameEdit.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Androids.makeText(R.string.please_enter_name_cardholder);
            return;
        }

        String deposit = depositBankEdit.getText().toString();
        if (TextUtils.isEmpty(deposit)) {
            Androids.makeText(R.string.please_enter_your_bank_account);
            return;
        }

        String address = openAccountAddressEdit.getText().toString();
        if (TextUtils.isEmpty(address)) {
            Androids.makeText(R.string.please_enter_your_account_address);
            return;
        }

        String number = bankCardNumberEdit.getText().toString();
        if (TextUtils.isEmpty(number)) {
            Androids.makeText(R.string.please_enter_the_bank_card_number);
            return;
        }

        String numberAgain = bankCardNumberAgainEdit.getText().toString();
        if (TextUtils.isEmpty(numberAgain)) {
            Androids.makeText(R.string.message_two_bank_card_numbers_different);
            return;
        }

        SendVerificationCodeDialog.show(this, textView ->
                        sendMessage((TextView) textView)
                , result -> submitBankCard(result.getCode()));
    }

    private void loadBankCardInfo() {
        int pageType = getIntent().getIntExtra(C.EXTRA_PAGE_TYPE, -1);
        ((TextView) findViewById(R.id.title_text)).setText(R.string.modify_bank_card);
        if (pageType == C.VALUE_MODIFY) {
            mPaymentViewModel.bankCardInfo().observe(this, data -> {
                nameEdit.setText(data.getName());
                depositBankEdit.setText(data.getBank_info());
                bankCardNumberEdit.setText(data.getCard_num());
                bankCardNumberAgainEdit.setText(data.getCard_num());
                openAccountAddressEdit.setText(data.getBank_name());
            });
        }
    }

    private void sendMessage(TextView textView) {
        mPaymentViewModel.sendMessage(C.API_BANK_CARD_TYPE).observe(this, resource -> {
            if (resource.state == Resource.Status.LOADING) {
                textView.setClickable(false);
                textView.setBackgroundColor(Color.parseColor("#CCCCCC"));
                textView.setText(R.string.message_sending);
            } else {
                Androids.makeText(resource.message);
                if (resource.state == Resource.Status.SUCCESS) {
                    new TimeCount(textView).start();
                }
            }
        });
    }

    private void submitBankCard(String code) {
        String name = nameEdit.getText().toString();
        String deposit = depositBankEdit.getText().toString();
        String address = openAccountAddressEdit.getText().toString();
        String number = bankCardNumberEdit.getText().toString();
        String numberAgain = bankCardNumberAgainEdit.getText().toString();

        mPaymentViewModel.setBankPayment(name, deposit, address, number, numberAgain, code)
                .observe(this, resource -> {
                    if (resource.state == Resource.Status.LOADING) {
                        Androids.showProgressMessage(this, R.string.submission);
                    } else {
                        Androids.dismissProgressMessage();
                        Androids.makeText(resource.message);
                        if (resource.state == Resource.Status.SUCCESS) {
                            EventBus.getDefault().post(new SubmitBankCardEvent());
                            finish();
                        }
                    }
                });
    }
}
